package MyGO.controller;

import MyGO.Data.GlobalData;
import MyGO.property.Medicine;
import MyGO.service.IMedicineService;
import MyGO.service.impl.MedicineServiceImpl;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.KeyCode;
import javafx.scene.input.KeyEvent;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import lombok.Setter;

import java.util.List;

import static MyGO.utils.ArrayfilterUtils.ArrayfilterUtils.FilterMedicineByPinYinAndMedcineName;

public class AddMedicineControllor {
    @Setter
    private Stage MyStage;

    @Setter
    PrescriMedicationController prescriMedicationController;

    @FXML
    private Button b_add;
    @FXML
    private Button b_close;
    @FXML
    private TableColumn<Medicine, Number> codeCloumn;
    @FXML
    private TableColumn<Medicine, String> formatCloumn;
    @FXML
    private TableColumn<Medicine, String> manufacturerCloumn;
    @FXML
    private TableColumn<Medicine, String> medicineCodeCloumn;
    @FXML
    private TableColumn<Medicine, String> medicineNameCloumn;
    @FXML
    private TableColumn<Medicine, String> priceCloumn;
    @FXML
    private TableColumn<Medicine, String> unitCloumn;
    @FXML
    private TextField tf_pinyin;
    @FXML
    private TextField tf_medicineName;
    @FXML
    private TableView<Medicine> tv_medicine;

    private StringProperty tf_searchPinYin = new SimpleStringProperty();
    private StringProperty tf_searchName = new SimpleStringProperty();
    private IMedicineService medicineService = new MedicineServiceImpl();

    @FXML
    public void initialize() {
        List<Medicine> medicines = medicineService.getMedicineList();

        codeCloumn.setCellValueFactory(cellData ->
                new SimpleIntegerProperty(tv_medicine.getItems().indexOf(cellData.getValue()) + 1));
        medicineCodeCloumn.setCellValueFactory(new PropertyValueFactory<>("id"));
        medicineNameCloumn.setCellValueFactory(new PropertyValueFactory<>("name"));
        formatCloumn.setCellValueFactory(new PropertyValueFactory<>("format"));
        unitCloumn.setCellValueFactory(new PropertyValueFactory<>("unit"));
        priceCloumn.setCellValueFactory(new PropertyValueFactory<>("priceStr"));
        manufacturerCloumn.setCellValueFactory(new PropertyValueFactory<>("manufacturer"));
        tv_medicine.getItems().addAll(medicines);

        tf_medicineName.textProperty().bindBidirectional(tf_searchName);
        tf_pinyin.textProperty().bindBidirectional(tf_searchPinYin);

        tf_searchName.addListener((observable, oldValue, newValue) -> {
            String pinyinValue = tf_searchPinYin.getValue();
            List<Medicine> lms = FilterMedicineByPinYinAndMedcineName(medicines, newValue, pinyinValue);
            if (lms != null) {
                tv_medicine.getItems().clear();
                tv_medicine.getItems().addAll(lms);
            }
        });

        tf_searchPinYin.addListener((observable, oldValue, newValue) -> {
            String nameValue = tf_searchName.getValue();
            List<Medicine> lms = FilterMedicineByPinYinAndMedcineName(medicines, nameValue, nameValue);
            if (lms != null) {
                tv_medicine.getItems().clear();
                tv_medicine.getItems().addAll(lms);
            }
        });

    }



    @FXML
    void Add(ActionEvent event) {
        YES();
    }

    @FXML
    void Close(ActionEvent event) {
        Stage stage = (Stage) b_close.getScene().getWindow();
        stage.close();
    }

    @FXML
    void tf_searchNameReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.DOWN)) {
            // 表格获取焦点
            tv_medicine.requestFocus(); // 设置焦点，生成视觉效果，获取键盘输入
            tv_medicine.getSelectionModel().selectFirst();  // 调用TableView的选择模型，并选中第一行
        }
        if (event.getCode().equals(KeyCode.UP)) {
            if (tv_medicine.getSelectionModel().getSelectedIndex() == 0) {
                tf_medicineName.requestFocus();
                tf_medicineName.selectAll();
            }
        }
    }

    @FXML
    void tf_searchPinyinReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.DOWN)) {
            // 表格获取焦点
            tv_medicine.requestFocus(); // 设置焦点，生成视觉效果，获取键盘输入
            tv_medicine.getSelectionModel().selectFirst();  // 调用TableView的选择模型，并选中第一行
        }
        if (event.getCode().equals(KeyCode.UP)) {
            if (tv_medicine.getSelectionModel().getSelectedIndex() == 0) {
                tf_pinyin.requestFocus();
                tf_pinyin.selectAll();
            }
        }
    }

    @FXML
    void tv_ClickedMedicineReleased(MouseEvent event) {
        tv_medicine.requestFocus();
    }

    @FXML
    void tv_searchMedicineReleased(KeyEvent event) {
        if (event.getCode().equals(KeyCode.ENTER)) {
            YES();
        }
    }

    void YES() {
        Medicine medicine = tv_medicine.getSelectionModel().getSelectedItem();
        prescriMedicationController.getTv_medicine().getItems().add(medicine);
        GlobalData.trackForm.getMedicineForm().getMedicineNum().put(medicine.getId(), 0);
        GlobalData.trackForm.getMedicineForm().getMedicineUsed().put(medicine.getId(), "无");
        GlobalData.trackForm.getMedicineForm().getMedicines().add(medicine.getId());
//        prescriMedicationController.getPriceAll().setText(medicine.getPriceStr());
//        double price = 0.0;
//        String temp = prescriMedicationController.getPriceAll().getText();
//        if (temp != null) {
//            price = Double.parseDouble(temp);
//            prescriMedicationController.getPriceAll().clear();
//            prescriMedicationController.getPriceAll().setText(String.valueOf(price + medicine.getPrice()));
//        } else {
//            prescriMedicationController.getPriceAll().setText(medicine.getPriceStr());
//        }

//        if (GlobalData.trackForm != null) {
//            GlobalData.trackForm.getMedicineForm().addMedicine(medicine);
//        }
    }
}
